import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import { _HttpClient } from '@delon/theme';
import {HttpClient} from '@angular/common/http';
import {AppUrl} from '../../../app.url';

@Component({
  selector: 'app-campus-list',
  templateUrl: './campus-list.component.html',
    styleUrls: [ './campus-list.component.less'],
})
export class CampusListComponent implements OnInit {
    array = [ 1 ];
    filter = '筛选条件';
    totalCount: any;
    selectProv:number = -1;
    selectCity:number = -1;
    selectLevel = '';
    provList = [];
    cityList = [];
    uniList = [];
    campList =[];
    uniCampList: any = [];
    schCond ='';

    uniTmpList = [];
    campTmpList = [];

    // provList = [
    //     '不限',
    //     '北京',
    //     '天津',
    //     '河北',
    //     '山西',
    //     '内蒙古',
    //     '辽宁',
    //     '吉林',
    //     '黑龙江',
    //     '上海',
    //     '江苏',
    //     '浙江',
    //     '安徽',
    //     '福建',
    //     '江西',
    //     '山东',
    //     '河南',
    //     '湖北',
    //     '湖南',
    //     '广东',
    //     '广西',
    //     '海南',
    //     '重庆',
    //     '四川',
    //     '贵州',
    //     '云南',
    //     '西藏',
    //     '陕西',
    //     '甘肃',
    //     '青海',
    //     '宁夏',
    //     '新疆',
    //     '台湾',
    //     '香港',
    //     '澳门',
    //     '海外',
    //     '全国',
    //     '其他',
    // ];

    // cityList = [ '全部', '广州', '韶关', '深圳', '珠海', '汕头', '佛山', '江门', '湛江', '茂名', '肇庆',
    //     '惠州', '梅州', '汕尾', '河源', '阳江', '清远', '东莞', '中山', '潮州', '揭阳', '云浮'];

    recordList = [ '不限', '一本', '二本', '专科'];

//     campusList : Array< {
//         uniId: number,
//         uniName: string,
//         campus :Array<{
//             campId : number,
//             campName :string,
//         }>
//     }
// >

     cities= [];

    slides = [
        {
            image: 'assets/img/coffee1.jpg',
        },
        {
            image: 'assets/img/coffee1.jpg',
        },
        {
            image: 'assets/img/coffee1.jpg',
        },
        {
            image: 'assets/img/coffee1.jpg',
        }
    ];


    showCity(provId) {
        this.selectProv = provId;
            this.http.post(AppUrl.city.selectByID, {proid: provId }).subscribe(data => {
            console.log(data);
            let temp: any = data;
            this.cityList = temp.data;
        });
    }

    selProv(provid){
        this.selectProv = provid;
        // this.filterInfo();
        this.search(1);
    }
    selCity(cityid){
        this.selectCity = cityid;
        // this.filterInfo();
        this.search(1);
    }
    selLevel(levelid){
        this.selectLevel = levelid;
        // this.filterInfo();
        this.search(1);
    }

    filterInfo() {

        this.campList =[];
        this.uniCampList =[];
        this.totalCount = 0 ;
        let tempList =[];
         tempList = this.uniTmpList;
        console.log("uniTmpList:"+this.uniTmpList);
        if(this.selectProv != -1){
            tempList = tempList.filter(e=> e.proid==this.selectProv);
        }
        if(this.selectCity != -1){
            tempList =tempList.filter(e=> e.cityid ==this.selectCity);
        }
        // if(this.selectLevel != ''){
        //     tempList =tempList.filter(e=> e.cityid==this.selectLevel);
        // }
        if(this.schCond != null){
            tempList =tempList.filter(e=> e.universityname ==this.schCond);
        }
        this.uniList =[];
        this.uniList = tempList;
        console.log( " this.uniList:"+ this.uniList);
        for(let uni of this.uniList){

            let temp: any = this.campTmpList.filter(e=> e.universityid===uni.id);
            this.uniCampList.push({
                uniId: uni.id,
                uniName: uni.universityname,
                campus: temp,
            });
            console.log(this.uniList);
            console.log(this.uniCampList);
            this.totalCount = this.uniCampList.length;

        }
    }

    private page: any = {
        pageNum: 1,
        pageSize: 12,
        total: 0,
    };

    search(pageNum){
        // if(this.selectProv == ''){
        //    alert("请选择城市");
        //    return;
        // }
        // if(this.selectCity ==''){
        //
        // }
        this.http.post(AppUrl.univeristy.univListByMultsUrl, {pageNo: pageNum, pageSize: this.page.pageSize,provId:this.selectProv,cityId:this.selectCity,univName:this.schCond}).subscribe(data => {
            console.log(data);
            let temp: any = data;
            this.page = temp.data;
            this.uniList = temp.data.list;
            this.uniCampList =[];

            for(let uni of this.uniList){
                this.totalCount = uni.univTotal;
                let temp: any = this.campList.filter(e => e.universityid === uni.univId);
                this.uniCampList.push({
                    uniId: uni.univId,
                    uniName: uni.univName,
                    campus: temp,
                });
            }

        });
    }

    constructor(
        private http: HttpClient, private ref: ChangeDetectorRef
    ) {

        let _that = this;
        this.http.post(AppUrl.province.selectAllUrl, { }).subscribe(data => {
            console.log(data);
            let temp: any = data;
            this.provList = temp.data;
        });

        // this.http.post(AppUrl.univeristy.univSelectAllUrl, { }).subscribe(data => {
        //     console.log(data);
        //     let temp: any = data; //全部学校列表
        //     this.uniList = temp.data;
        //     this.uniTmpList = temp.data;
        //     this.http.post(AppUrl.campus.campSelectAll, { }).subscribe(data => {
        //         console.log(data);
        //         let temp: any = data; //全部社团列表
        //         this.campList = temp.data;
        //         this.campTmpList = temp.data;
        //         console.log(this.uniList);
        //         for(let uni of this.uniList){
        //
        //             let temp: any = this.campList.filter(e=> e.universityid===uni.id);
        //             _that.campusList.push({
        //                 uniId: uni.id,
        //                 uniName: uni.universityname,
        //                 campus: temp,
        //             });
        //             console.log(this.uniList);
        //             console.log(_that.campusList);
        //             _that.totalCount = _that.campusList.length;
        //
        //         }
        //
        //     });
        // });


        this.http.post(AppUrl.univeristy.univListByMultsUrl, {pageNo: 1, pageSize: this.page.pageSize,provId:this.selectProv,cityId:this.selectCity,univName:this.schCond}).subscribe(data => {
            console.log(data);
            let temp: any = data; //符合条件的全部学校列表
            this.page= temp.data;
            this.uniList = temp.data.list;
            this.uniCampList =[];

            this.http.post(AppUrl.campus.campSelectAll, { }).subscribe(data => {
                let temp: any = data; //全部社团列表
                this.campList = temp.data;
                this.uniCampList =[];

                for (let uni of this.uniList) {
                    this.totalCount = uni.univTotal;
                    let temp: any = this.campList.filter(e => e.universityid === uni.univId);
                    this.uniCampList.push({
                        uniId: uni.univId,
                        uniName: uni.univName,
                        campus: temp,
                    });
                }
            });
        });

    }

    ngOnInit() {
    }
}
